﻿using BDRUtils.Utility;
using System;

namespace BDRUtils.Extensions
{
    public static class StringExtensions
    {
        /// <summary>
        /// 判断字符串是否为空
        /// </summary>
        /// <param name="value"></param>
        /// <returns><see cref="string.IsNullOrWhiteSpace(string)">  </returns>
        public static bool IsNullOrWhiteSpace(this string value) => string.IsNullOrWhiteSpace(value);

        /// <summary>
        /// 转换Guid
        /// </summary>
        /// <param name="value"></param>
        /// <param name="isNew">若为true则生成新的Guid</param>
        /// <returns></returns>
        public static Guid TogGuid(this string value, bool isNew = false)
        {
            if (!value.IsNullOrWhiteSpace() && Guid.TryParse(value, out Guid result))
                return result;

            return isNew ? Guid.NewGuid() : Guid.Empty;
        }

        /// <summary>
        /// 判断Guid格式是否正确
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsGuid(this string str)
        {
            return Guid.TryParse(str, out _);
        }

        /// <summary>
        /// 转换成当前文化的小写
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToCultureLower(this string str)
        {
            return str.NullToString().ToLower(System.Globalization.CultureInfo.CurrentCulture);
        }

        /// <summary>
        /// Hex编码
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string ToHexString(this byte[] bytes)
        {
            if (bytes == null) return string.Empty;
            var result = "";
            for (int i = 0; i < bytes.Length; i++)
            {
                result += bytes[i].ToString("x2");
            }
            return result;
        }

        /// <summary>
        /// 转换Int(int.TryParse)
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int ToInt(this string value)
        {
            if (value.IsNullOrWhiteSpace() || !int.TryParse(value, out int result))
                return -1;
            return result;
        }

        /// <summary>
        /// 转兼容路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string ToCompatibilityPath(this string path)
        {
            return FileInfoUtil.Combine(path);
        }

        /// <summary>
        /// SQL入参的特殊字符转义
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ReplaceSQLChar(this string str)
        {
            if (str == String.Empty)
                return String.Empty;
            str = str.Replace("'", "''");
            str = str.Replace(";", "\\;");
            str = str.Replace(",", "\\,");
            str = str.Replace("?", "\\?");
            str = str.Replace("<", "\\<");
            str = str.Replace(">", "\\>");
            str = str.Replace("(", "\\(");
            str = str.Replace(")", "\\)");
            str = str.Replace("@", "\\@");
            str = str.Replace("=", "\\=");
            str = str.Replace("+", "\\+");
            str = str.Replace("*", "\\*");
            str = str.Replace("&", "\\&");
            str = str.Replace("#", "\\#");
            str = str.Replace("%", "\\%");
            str = str.Replace("$", "\\$");
            return str;
        }
    }
}
